package com.vhall.component.service.thirduser.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vhall.component.dao.thirduser.ThirdUserMapper;
import com.vhall.component.entity.account.GuosenStaffEntity;
import com.vhall.component.entity.thirduser.entity.ThirdUserEntity;
import com.vhall.component.entity.thirduser.enums.ThirdUserTypeEnum;
import com.vhall.component.service.account.GuosenStaffService;
import com.vhall.component.service.thirduser.ThirdUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author xing.wang
* @description 针对表【third_user(三方用户表-包括关联的主播,运营用户基本信息)】的数据库操作Service实现
* @createDate 2023-08-09 14:15:34
*/
@Service
public class ThirdUserServiceImpl extends ServiceImpl<ThirdUserMapper, ThirdUserEntity>
    implements ThirdUserService {
    @Resource
    private GuosenStaffService guosenStaffService;
    @Override
    public List<ThirdUserEntity> getThirdUserEntitiesBatchSaveOrUpdate(List<String> staffUserCode) {
        if (CollUtil.isEmpty(staffUserCode)) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<GuosenStaffEntity> guosenStaffWhere = Wrappers.lambdaQuery();
        guosenStaffWhere.in(GuosenStaffEntity::getNotesid, staffUserCode);
        List<GuosenStaffEntity> staffList = guosenStaffService.list(guosenStaffWhere);
        List<ThirdUserEntity> thirdUserEntities = staffList.stream().map(this::buildGuosenStaffToThirdUser).collect(Collectors.toList());
        LambdaQueryWrapper<ThirdUserEntity> thirdUserWhere = Wrappers.lambdaQuery();
        thirdUserWhere
                .eq(ThirdUserEntity::getUserType, ThirdUserTypeEnum.STAFF.getValue())
                .in(ThirdUserEntity::getUserCode, staffUserCode);
        //已经存在的用户
        List<ThirdUserEntity> thirdUserDataExists = list(thirdUserWhere);
        if (CollUtil.isNotEmpty(thirdUserDataExists)) {
            Map<String, Integer> userCodeToId = thirdUserDataExists.stream().collect(Collectors.toMap(ThirdUserEntity::getUserCode, ThirdUserEntity::getId,(oldValue, newValue) -> newValue));
            Set<String> thirdUserDataExistsUserCodes = userCodeToId.keySet();
            thirdUserEntities = thirdUserEntities.stream().map(data->{
                if(thirdUserDataExistsUserCodes.contains(data.getUserCode())){
                    data.setId(userCodeToId.get(data.getUserCode()));
                }
                return data;
            }).collect(Collectors.toList());
            saveOrUpdateBatch(thirdUserEntities);
        }else{
            saveOrUpdateBatch(thirdUserEntities);
        }
        return thirdUserEntities;
    }
    private ThirdUserEntity buildGuosenStaffToThirdUser(GuosenStaffEntity data) {
        ThirdUserEntity thirdUser = new ThirdUserEntity();
        thirdUser.setUserType(ThirdUserTypeEnum.STAFF.getValue());
        thirdUser.setAdvisorLicenseNo(data.getCyzgzbh());
        thirdUser.setUserCode(data.getNotesid());
        thirdUser.setName(data.getStaffname());
        thirdUser.setOrgCode(data.getOrgcode());
        thirdUser.setOrgName(data.getUnits());
        return thirdUser;
    }
}




